package com.zmx.product.service;

import com.nepxion.aquarius.cache.annotation.Cacheable;
import com.zmx.common.common.contents.cache.Expire;
import com.zmx.common.common.service.BaseService;
import com.zmx.common.common.contents.cache.ProductCacheConstents;
import com.zmx.product.dto.ProductDto;
import com.zmx.product.entity.Product;
import com.zmx.product.repository.ProductRepository;
import com.zmx.product.service.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
@author zhaomxr
*/
@Service
public class ProductService extends BaseService<Product, ProductDto, ProductRepository, ProductMapper>  {

    /** 商品信息暂时使用分布式缓存，过期时间设为30分钟，商品数量过多就要考虑部分商品使用缓存 **/
    @Cacheable(name = ProductCacheConstents.PRODUCT, key = "#productId", expire = 18000000)
    public ProductDto findDtoById(Long productId) {
        Product product = repository.findById(productId);
        return entityToDto(product);
    }

    public List<ProductDto> findDtoByIdIn(Long[] productIds) {
        List<Product> products = repository.findByIdIn(productIds);
        return entityToDto(products);
    }

    /**
     * 根据商品id查询 同一种商品下不同规格所有商品
     * @param productId
     * @return
     */
    public List<ProductDto> getProductListByProductSn(Long productId) {
        List<Product> products = repository.getProductListByProductSn(productId);
        return entityToDto(products);
    }
}
